/*
#include <stack>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <sstream>
using namespace std;
struct ListNode{
	int val;
	ListNode *next;
	ListNode(int x):val(x),next(NULL){}
};
ListNode* reverseBetween(ListNode* head, int m, int n) {
	if(n<=m)
		return head;
	ListNode *pre=NULL,*p1,*p2=NULL,*p3=NULL,*p4=NULL;
	int num=1;
	p1=head;
	bool flag=true;
	while(p1){
		if(num==m){
			p2=pre;
		}else if(num>m&&num<=n){
			if(p4==NULL)
				p4=pre;
			p3=p1->next;
			p1->next=pre;
			pre=p1;
			p1=p3;
			num++;
			continue;
		}else if(num>n){
			flag=false;
			if(p2==NULL){
				head=pre;
				p4->next=p1;
			}
			else{
				p2->next=pre;
				p4->next=p1;
			}
			break;
		}
		pre=p1;
		p1=p1->next;
		num++;
	}
	if(flag){
		if(p2==NULL){
			head=pre;
		}else{
			p2->next=pre;
		}
		if(p4!=NULL)
			p4->next=NULL;
	}
	return head;
}
ListNode *buildList(vector<int> &nums1){
	ListNode *head=NULL,*p,*q;
	p=head=new ListNode(nums1[0]);
	for(int i=1;i<nums1.size();i++){
		q=new ListNode(nums1[i]);
		p->next=q;
		p=q;
	}
	return head;
}
int main(){
	int a[]={1,2,3,4,5};
	vector<int> nums1(a,a+sizeof(a)/sizeof(int));
	ListNode *head=buildList(nums1);
	reverseBetween(head,6,8);
	cout<<endl;
	system("pause");
}
*/